﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

namespace WEALTH
{
    public partial class List : Page
    {
        int CurrentPage;//当前页数
        int PageSize;   //每页条数
        int PageCount;  //总页数
        int RecordCount;//总条数
        public string strLeftmenu = string.Empty;
        private ExcuteDao dao = new ExcuteDao();
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            PageSize = 10;//每页10条记录
            //menutype 数据是一级菜单名称列如：菜单上的【关于我们】
            if (!string.IsNullOrEmpty(Request.QueryString["menutype"]))
            {
                SetLeftMenu();
            }

            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["menuid"]))
                {
                    CurrentPage = 0;//当前页习惯设为0
                    ViewState["PageIndex"] = 0;//页索引也设为0

                    DataTable dt = dao.Get_List(Convert.ToInt32(Request.QueryString["menuid"]));
                    //if (dt != null && dt.Rows.Count == 1)
                    //{
                    //    Response.Redirect("ListView.aspx");
                    //    return;
                    //}

                    ViewState["PageData"] = dt;
                    //计算总共有多少记录
                    RecordCount = dt.Rows.Count;

                    //计算总共有多少页
                    if (RecordCount % PageSize == 0)
                    {
                        PageCount = RecordCount / PageSize;
                    }
                    else
                    {
                        PageCount = RecordCount / PageSize + 1;
                    }

                    //this.TotalLbl.Text = "总" + PageCount.ToString() + "页";//显示总页数
                    ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ，而视图状态 viewstate相当于某个页面的 session

                    this.DataListBind();//不可以放在初始化条件之前就绑定，那样的话，如果仅有一页的数据，“下一页”页仍然显示
                }
            }
        }

        public string GetTitleStr(object title)
        {
            return AppFunction.GetString(title.ToString(), 60);
        }

        public string GetjianjieStr(object jianjie)
        {
            return AppFunction.GetString(jianjie.ToString(), 180);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataListBind();
        }

        //将数据绑定到Datalist控件
        public void DataListBind()
        {
            try
            {
                AspNetPager1.PageSize = 10;
                //初始化分页数据源实例            
                PagedDataSource pds = new PagedDataSource();
                //设置总行数           
                AspNetPager1.RecordCount = ((DataTable)ViewState["PageData"]).DefaultView.Count;
                //设置分页的数据源          
                pds.DataSource = ((DataTable)ViewState["PageData"]).DefaultView;
                //设置当前页         
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //设置每页显示页数           
                pds.PageSize = AspNetPager1.PageSize;
                //启用分页         
                pds.AllowPaging = true;
                //设置GridView的数据源为分页数据源          
                DataList1.DataSource = pds;
                this.DataList1.DataBind();
                //this.PreviousLB.Enabled = true;
                //this.NextLB.Enabled = true;
                //if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时，下一页链接按钮不可用
                //if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时，上一页按钮不可用
                //this.CurrentLbl.Text = "当前第" + (CurrentPage + 1).ToString() + "页";//当前页数

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
        {
            CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
            PageCount = (int)ViewState["PageCount"];//获得总页数

            string cmd = e.CommandName;

            //判断cmd，以判定翻页方向
            switch (cmd)
            {
                case "prev"://上一页
                    if (CurrentPage > 0) CurrentPage--;
                    break;

                case "next":
                    if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
                    break;

                case "first"://第一页
                    CurrentPage = 0;
                    break;

                case "end"://最后一页
                    CurrentPage = PageCount - 1;
                    break;

                //case "jump"://跳转到第几页
                //if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
                //{
                //    return;
                //}
                //else
                //{
                //    CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
                //    break;
                //}
            }
            ViewState["PageIndex"] = CurrentPage;//获得当前页

            this.DataListBind();//重新将DataList绑定到数据库
        }
        /// <summary>
        /// 设置左侧菜单
        /// </summary>
        private void SetLeftMenu()
        {
            string strmenutype = Request.QueryString["menutype"].ToString();

            DataTable dt = dao.Get_LeftMenuALL("", strmenutype);
            strLeftmenu = string.Empty;
            strLeftmenu += "<li id=\"one\" class=\"files\"><a href=\"#\">" + strmenutype + "</a><ul class=\"sub-menu\">";

            if (strmenutype.Equals("投资者教育"))
            {
                strLeftmenu += "<li><a href=\"Risk.aspx\" title=\"\"><em>01</em>在线风险测试</a></li>";
                strLeftmenu += "<li><a href=\"Assets.aspx\" title=\"\"><em>02</em>在线资产配置</a></li>";
                strLeftmenu += "<li><a href=\"ListView.aspx?listviewid=15&menutype=投资者教育\" title=\"\"><em>03</em>理财师的价值</a></li>";
                strLeftmenu += "<li><a href=\"#\" title=\"\"><em>04</em>在线理财师</a></li>";
                strLeftmenu += "<li><a href=\"MoneyManageOrder.aspx\" title=\"\"><em>05</em>预约理财师</a></li>";
                strLeftmenu += "<li><a href=\"#\" title=\"\"><em>06</em>理财计算器</a></li>";
            }
            else
            {
                #region 子菜单
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int intMenuID = Convert.ToInt32(dt.Rows[i]["MenuID"]);
                        string strmenuname = dt.Rows[i]["MenuName"].ToString();
                        DataTable menuDt = dao.Get_ListALL("", "", intMenuID, "");

                        if (menuDt.Rows.Count == 1)
                        {
                            int listid = Convert.ToInt32(menuDt.Rows[0]["listID"]);

                            strLeftmenu += "<li><a href=\"ListView.aspx?listviewid="
                            + listid + "&menutype=" + strmenutype + "\" title=\"\"><em>"
                            + (i + 1).ToString().PadLeft(2, '0') + "</em>" + strmenuname + "</a></li>";
                        }
                        else
                        {
                            strLeftmenu += "<li><a href=\"List.aspx?menuid="
                                + intMenuID + "&menutype=" + strmenutype + "\" title=\"\"><em>"
                                + (i + 1).ToString().PadLeft(2, '0') + "</em>" + strmenuname + "</a></li>";
                        }
                        //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>企业文化</a></li>";
                        //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>精英团队</a></li>";
                        //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>公司新闻</a></li>";
                        //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>合作伙伴</a></li>";
                        //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>招贤纳士</a></li>";
                        //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>联系我们</a></li>";
                    }
                }
                #endregion
            }
            strLeftmenu += "</ul></li>";
        }


    }
}